# !/usr/bin/python
# -* - coding: UTF-8 -* -
import sys

# 对于不满4位的2进制字符串，前面补0
def bin_format(string):
    length = len(string)
    if(length==4):
      return string
    elif(length<4):
      prefix_string = "0"
      prefix_length = 4-length
      return prefix_string*prefix_length + string
    else:
      print("err in bin_format: " + string)
      return "FFFF"

# 将传入的16进制opcode，转换为2进制字符串
def str_to_bin(string):
    bin_str = ''
    for char in string:
        if(ord(char)>=48 and ord(char)<=57):
          intChar = ord(char)-48
        elif(ord(char)>=65 and ord(char)<=90):
          intChar = ord(char)-55
        elif(ord(char)>=97 and ord(char)<=122):
          intChar = ord(char)-87
        else:
          print("err in str_to_bin: " + string)
          intChar = "0"
        binChar = format(intChar, 'b')
        bin_str += bin_format(binChar)
    return bin_str

# 0100 0000 1100 1000 0000 0000 0000 0000
def opcode_analy(string):
    major_str = string[3:10]
    major_int = int(major_str, 2)
    print("\n\033[0m系统性能参数的大类: " + major_str + ", \t\t\033[96mmajor_value: ", major_int)
    min_str = string[12:18]
    min_int = int(min_str, 2)
    print("\n\033[0m系统性能参数的具体子项: " + min_str + ", \t\033[96mmin_value: ", min_int)
    map_str = string[18]
    map_int = int(map_str, 2)
    print("\n\033[0mmap_str: " + map_str + ", \t\t\t\t\033[96mmap_value: ", map_int)
    group_str = string[20:24]
    group_int = int(group_str, 2)
    print("\n\033[0mcpu的核簇: " + group_str + ", \t\t\t\033[96mgroup_value: ", group_int)
    core_str = string[25:28]
    core_int = int(core_str, 2)
    print("\n\033[0m具体的cpucore: " + core_str + ", \t\t\t\033[96mcore_value: ", core_int)

# 0x40C80000
opcode = input("16进制opcode:\033[96m \t\t\t\t")
# print(opcode)
length = len(str(opcode))
# print(length)
if(length == 10):
  opcode = opcode[2:]
elif(length != 8):
  print("err,opcode illegal ! opcode =",opcode)
  sys.exit()
# 40C80000
binCode = str_to_bin(opcode)
if(len(binCode) != 32):
  print("binCode err(len != 32),binCode =",binCode)
print("\n\033[0m2进制opcode:\033[96m\t\t\t\t",binCode)
opcode_analy(binCode)
